package com.example.controller.tribune;

import com.example.common.Result;

import com.example.common.enums.RoleEnum;
import com.example.model.entity.other.Account;
import com.example.model.entity.tribune.Blog;
import com.example.model.entity.tribune.Post;
import com.example.model.entity.tribune.PostSuccess;
import com.example.service.tribune.BlogService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 社区帖子信息表前端操作接口
 **/
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    /**
     * 根据ID查询点赞收藏文字数量
     */
    @ApiOperation(value = "根据ID查询点赞收藏文字数量")
    @GetMapping("/selectSuccessById/{roleId}")
    public Result selectSuccessById(@PathVariable Integer roleId) {
        PostSuccess blogSuccess = blogService.selectSuccessById(roleId);
        return Result.success(blogSuccess);
    }
    @ApiOperation(value = "查询所有论坛帖子信息")
    @GetMapping("/selectFrontBlog/{typeId}")
    public Result selectFrontBlog(@PathVariable Integer typeId) {
        List<Blog> list = blogService.selectFrontBlog(typeId);
        return Result.success(list);
    }
    @PutMapping("/updateCount/{id}")
    public Result updateCount(@PathVariable Integer id) {
       blogService.updateCount(id);
        return Result.success();
    }
    /**
     * 前台分页查询
     */
    @GetMapping("/selectFrontPage")
    public Result selectFrontPage(Blog blog,
                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            blog.setRoleId(currentUser.getId());
        }
        PageInfo<Blog> page = blogService.selectFrontPage(blog, pageNum, pageSize);
        return Result.success(page);
    }
    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Blog blog) {
        blogService.add(blog);
        return Result.success();
    }

    @GetMapping("/selectTopBlog")
    public Result selectTopBlog(Blog blog) {
        List<Blog> list = blogService.selectTopBlog(blog);
        return Result.success(list);
    }
    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        blogService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        blogService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Blog blog) {
        blogService.updateById(blog);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Blog blog = blogService.selectById(id);
        return Result.success(blog);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Blog blog) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            blog.setRoleId(currentUser.getId());
        }
        List<Blog> list = blogService.selectAll(blog);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Blog blog,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            blog.setRoleId(currentUser.getId());
        }
        PageInfo<Blog> page = blogService.selectPage(blog, pageNum, pageSize);
        return Result.success(page);
    }

}
